package com.emm.yixun.customer.service.impl.app.manage;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.emm.yixun.common.model.enums.SubscribeBusinessStatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.basic.service.BusinessAbnormaService;
import com.emm.yixun.basic.service.CustomerService;
import com.emm.yixun.basic.service.SubscribeService;
import com.emm.yixun.common.app.GetSubscribeListReqDto;
import com.emm.yixun.common.app.GetSubscribeListResDto;
import com.emm.yixun.common.app.servletentity.SubscribeListRes;
import com.emm.yixun.common.model.BusinessAbnorma;
import com.emm.yixun.common.model.Customer;
import com.emm.yixun.common.model.dto.BusinessAbnormaDto;
import com.emm.yixun.common.model.dto.SubscribeDto;
import com.emm.yixun.common.model.enums.AuditStatusEnum;
import com.emm.yixun.common.model.enums.BusinessAbnormaEnum;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.page.PageUtil;

@Service("getSubscribeListManage")
public class GetSubscribeListManage {

	@Autowired
	private CustomerService customerService;
	@Autowired
	private SubscribeService subscribeService;
	@Autowired
	private BusinessAbnormaService businessAbnormaService;
	
	/**
	 * 认购列表
	 * @param reqDto
	 * @return
	 */
	public GetSubscribeListResDto getSubscribeList(GetSubscribeListReqDto reqDto) {
		GetSubscribeListResDto resDto = new GetSubscribeListResDto();
		try{
			//1、获取客户相关的姓名及联系电话
			Customer customer = customerService.selectById(Long.parseLong(reqDto.getCustomerId()));
			if(customer != null){
				resDto.setCustomerName(customer.getCustomerName());
				resDto.setCustomerPhone(customer.getCustomerPhone());
			}else{
				resDto.setErrorCode("0000");
				resDto.setErrorMsg("客户获取失败");
				resDto.setResult("0");
				return resDto;
			}
			//2、获取客户认筹列表
			SubscribeDto subscribeDto = new SubscribeDto();
			subscribeDto.setMerchantId(reqDto.getMerchantId());
			subscribeDto.setProjectCode(reqDto.getProjectCode());
			subscribeDto.setCustomerId(Long.parseLong(reqDto.getCustomerId()));
			String pageNumber = reqDto.getPageNumber();
			String pageSize = reqDto.getPageSize();
			subscribeDto.setOrders(" t.create_time desc ");
			Page<SubscribeDto> subscribeDtoPage = subscribeService.getSubscribeList(subscribeDto, PageUtil.begin(Integer.parseInt(pageNumber) , Integer.parseInt(pageSize)), Integer.parseInt(pageSize));
			resDto.setResult("1");
			resDto.setPageNumber(pageNumber);
			resDto.setPageSize(pageSize);
			resDto.setTotalPage(String.valueOf(subscribeDtoPage.getTotalPages()));
			resDto.setTotalCount(String.valueOf(subscribeDtoPage.getTotal()));
			List<SubscribeDto> dtoList = subscribeDtoPage.getRows();
			List<SubscribeListRes> subscribeList = new ArrayList<SubscribeListRes>();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			for(SubscribeDto dto: dtoList){
				SubscribeListRes pdlRes = new SubscribeListRes();
				pdlRes.setSubscribeId(String.valueOf(dto.getId()));
				pdlRes.setSubscribeHouses(dto.getSubscribeHouses());
				if("1".equals(dto.getDiscountApproval())){
					BusinessAbnormaDto businessAbnormaDto = new BusinessAbnormaDto();
					businessAbnormaDto.setBusinessId(dto.getId());//业务ID
					businessAbnormaDto.setBusinessType(BusinessAbnormaEnum.BusinessAbnorma_0.getNo());
					businessAbnormaDto.setOrders(" t.id desc");//id降序取第一条
					List<BusinessAbnorma> businessAbnormaList = businessAbnormaService.selectListByDto(businessAbnormaDto);
					if(businessAbnormaList.size() > 0){
						for(int i = 0 ;i<businessAbnormaList.size();i++){
							if(!"3".equals(businessAbnormaList.get(0).getAuditStatus())){
								pdlRes.setAuditStatus(AuditStatusEnum.getByNo(businessAbnormaList.get(0).getAuditStatus()).getDes());//0待提交 1审核中 2已退回 3已通过
							}else{
								if(dto.getAuditStatus() != null && !"".equals(dto.getAuditStatus())){
									pdlRes.setAuditStatus(AuditStatusEnum.getByNo(dto.getAuditStatus()).getDes());
								}else{
									pdlRes.setAuditStatus(null);
								}
							}
						}
					}else{
						if(dto.getAuditStatus() != null && !"".equals(dto.getAuditStatus())){
							pdlRes.setAuditStatus(AuditStatusEnum.getByNo(dto.getAuditStatus()).getDes());
						}else{
							pdlRes.setAuditStatus(null);
						}
					}
				}else{
					if(dto.getAuditStatus() != null && !"".equals(dto.getAuditStatus())){
						pdlRes.setAuditStatus(AuditStatusEnum.getByNo(dto.getAuditStatus()).getDes());
					}else{
						pdlRes.setAuditStatus(null);
					}
				}
				if(dto.getDidTime() != null && !"".equals(dto.getDidTime())){
					pdlRes.setSubscribeTime(sdf.format(dto.getDidTime()));
				}else{
					pdlRes.setSubscribeTime(null);
				}
				pdlRes.setRecordBusiness("认购");
				if(AuditStatusEnum.REJECT.getNo().equals(dto.getAuditStatus())){
					pdlRes.setRecordBusiness("认购驳回");
				}else if(AuditStatusEnum.SUCCESS.getNo().equals(dto.getAuditStatus())){
					String businessStatus = dto.getBusinessStatus();
					String businessSecStatus = dto.getBusinessSecStatus();
					if(org.apache.commons.lang.StringUtils.isNotBlank(businessSecStatus)){
						if(SubscribeBusinessStatusEnum.REFUNDSUBSCRIPTION.getNo().equals(businessSecStatus)){
							pdlRes.setRecordBusiness("认购退房");
						}else {
							pdlRes.setRecordBusiness("认购");
						}
					}else{
						if(SubscribeBusinessStatusEnum.REFUNDSUBSCRIPTION.getNo().equals(businessStatus)){
							pdlRes.setRecordBusiness("认购退房");
						}else {
							pdlRes.setRecordBusiness("认购");
						}
					}
				}

				subscribeList.add(pdlRes);
			}
			resDto.setSubscribeList(subscribeList);
			resDto.setResult("1");
		}catch(Exception e){
			e.printStackTrace();
			resDto.setErrorCode("00000");
			resDto.setErrorMsg("认购列表获取失败!");
			resDto.setResult("0");
			return resDto;
		}
		return resDto;
	}
}
